<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!--      <span>查询模式</span>-->
      <el-form-item label="查询模式">
        <el-select v-model="value" style="width: 100px">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <div v-if="this.value === '选项1'" style="display: inline-block">
        <el-form-item>
          <el-select v-model="selectedDateRange" style="width: 100px" @change="setDiyDateRange(selectedDateRange)">
            <el-option
              v-for="item in options1"
              :key="item.value1"
              :label="item.label1"
              :value="item.value1"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="selectedDateRange === '自定义'">
          <el-date-picker
            v-model="dateRange"
            type="daterange"
            align="right"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="setDiyDateRange(dateRange)"
          >
          </el-date-picker>
        </el-form-item>
      </div>
      <!--      按天-->
      <el-form-item v-if="this.value === '选项2'">
        <el-date-picker
          v-model="selectedDay"
          type="date"
          style="width: 140px;"
          placeholder="选择日期"
          @change="setDay"
        >
        </el-date-picker>
      </el-form-item>
      <!--      按月-->
      <el-form-item v-if="this.value === '选项3'">
        <el-date-picker
          v-model="month"
          type="month"
          style="width: 160px;"
          placeholder="选择月"
          @change="setMonth"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <!--        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>-->
      </el-form-item>
    </el-form>

    <el-button
      icon="el-icon-s-operation"
      @click="drawer = true"
      type="primary"
      style="position: absolute; right: 105px; top: 24px; font-size: 14px; height: 27px; line-height: 27px; margin: 0; padding: 0 7px"
    >
      筛选
    </el-button>

    <el-button
      style="position: absolute; right: 25px; top: 22px"
      type="warning"
      plain
      icon="el-icon-download"
      size="mini"
      @click="handleExport"
      v-hasPermi="['monitor:operlog:export']"
    >导出
    </el-button>

    <el-drawer
      title="我是标题"
      :visible.sync="drawer"
      :direction="direction"
      :with-header="false"
      size="25%"
    >
      <el-tabs v-model="activeName" type="card">
        <el-tab-pane label="筛选车辆" name="first">
          <div style="padding-left: 5%">
            <el-input
              v-model="input"
              placeholder="搜索车辆"
              suffix-icon="el-icon-search"
              style="width: 90%; margin-bottom: 15px"
            >
            </el-input>
            <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange">全选
            </el-checkbox>
            <div style="margin: 8px 0;"></div>
            <el-checkbox-group v-model="queryParams.carNoList" @change="handleCheckedCarsChange">
              <el-checkbox
                v-for="item in carList"
                :label="item.car_no"
                :key="item.online_id"
                style="margin-bottom: 5px"
              >
                {{ item.car_no }}
              </el-checkbox>
            </el-checkbox-group>
          </div>
        </el-tab-pane>
        <el-tab-pane
          label="筛选报警"
          name="second"
          disabled
        >
        </el-tab-pane>
        <el-tab-pane label="筛选司机" name="third">
          <div style="padding-left: 5%">
            <el-input
              v-model="input"
              placeholder="搜索司机"
              suffix-icon="el-icon-search"
              style="width: 90%;"
            >
            </el-input>
          </div>
        </el-tab-pane>
      </el-tabs>
    </el-drawer>

    <el-table ref="tables" v-loading="loading" :data="list"
              :default-sort="defaultSort" @sort-change="handleSortChange">
      <el-table-column label="日期" align="center" prop="startTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="车牌号" align="center" prop="carNo"/>
      <el-table-column label="驾驶员" align="center" prop="driverName"/>
      <el-table-column label="公司名称" align="center" prop="compName"/>
      <el-table-column label="行车里程/公里" align="center" prop="mileage" width="130" :show-overflow-tooltip="true"/>
      <el-table-column label="行车时长/小时" align="center" prop="duration" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span>{{ (scope.row.duration/60).toFixed(1) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="开始时间" align="center" prop="startTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="开始里程/公里" align="center" prop="startMileage"/>
      <el-table-column label="结束里程/公里" align="center" prop="endMileage"/>
      <el-table-column label="当天轨迹" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleView(scope.row,scope.index)"
            v-hasPermi="['monitor:operlog:query']">查看
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNumber"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import {drive} from '../../../api/info/car'
import { getCarSelectItems } from '../../../api/info/car'

export default {
  name: 'day',
  dicts: ['sys_oper_type', 'sys_common_status'],
  data() {
    return {
      checkAll: false,
      // cities: cityOptions,
      isIndeterminate: false,
      //筛选车辆输入框
      input: '',
      //tabs标签页
      activeName: 'first',
      //抽屉
      drawer: false,
      direction: 'rtl',
      //查询按天
      selectedDay: new Date(),
      //查询按月
      month: new Date(),
      //自定义时间范围
      // 日期范围
      //el-date选择器选定时间范围
      dateRange: [],
      //下拉框选项时间范围
      selectedDateRange: '',
      //查询模式
      options: [{
        value: '选项1',
        label: '自选区'
      }, {
        value: '选项2',
        label: '按天'
      }, {
        value: '选项3',
        label: '按月'
      }],
      value: '选项2',
      options1: [{
        value1: '近3天',
        label1: '近三天'
      }, {
        value1: '近7天',
        label1: '近七天'
      }, {
        value1: '近15天',
        label1: '近15天'
      }, {
        value1: '近30天',
        label1: '近30天'
      }, {
        value1: '自定义',
        label1: '自定义'
      }],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      //车辆列表
      carList: [],
      // 是否显示弹出层
      open: false,
      // 默认排序
      defaultSort: { prop: 'operTime', order: 'descending' },
      // 表单参数
      form: {},
      // 查询参数
      queryParams: {
        endTime: new Date().setHours(0, 0, 0, 0),
        startTime: new Date().setHours(0, 0, 0, 0) + 1000 * 60 * 60 * 24 - 1,
        carNoList: [],
        pageNumber: 1,
        pageSize: 10,
        orderByColumn: '',
        isAsc: ''
      }
    }
  },
  created() {
    this.getList()
    this.getCarList()
  },
  methods: {
    //筛选车辆 复选框
    handleCheckAllChange() {
      if (this.queryParams.carNoList.length < this.carList.length) {
        this.queryParams.carNoList = []
        this.carList.forEach(item => {
          this.queryParams.carNoList.push(item.car_no)
        })
        this.isIndeterminate = false
      } else {
        this.queryParams.carNoList = []
        this.isIndeterminate = true
      }
    },
    handleCheckedCarsChange(value) {
      let checkedCount = value.length
      this.checkAll = checkedCount === this.carList.length
      this.isIndeterminate = checkedCount > 0 && checkedCount < this.carList.length
    },

    setDay() {
      if (this.selectedDay === undefined || this.selectedDay === null) {
        return
      }
      this.queryParams.startTime = this.selectedDay.setHours(0, 0, 0, 0)
      this.queryParams.endTime = this.selectedDay.setHours(0, 0, 0, 0) + 1000 * 60 * 60 * 24 - 1
    },

    setMonth() {
      if (this.month === undefined || this.month === null) {
        return
      }
      let currentMonth = this.month.getMonth()
      let nextMonth = currentMonth + 1
      this.queryParams.startTime = this.month.getTime()
      this.queryParams.endTime = new Date(this.month.getTime()).setMonth(nextMonth) - 1
    },

    setDiyDateRange(range) {
      if (range === undefined || range === '' || range === null) {
        return
      }
      console.log(range)
      if (range instanceof Array) {
        this.queryParams.startTime = range[0].getTime()
        this.queryParams.endTime = range[1].getTime()
      } else {
        if (range !== '自定义') {
          let dayDiff = Number(range.substr(1, range.length - 2)) - 1
          this.queryParams.startTime = new Date().setHours(0, 0, 0, 0) - 1000 * 60 * 60 * 24 * dayDiff
          this.queryParams.endTime = new Date().setHours(0, 0, 0, 0) + 1000 * 60 * 60 * 24 - 1
        }
      }
    },

    getList() {
      this.loading = true
      drive(this.queryParams).then(response => {
          console.log('=response=', response)
          this.list = response.dataList
          this.total = response.data.total
          this.loading = false
        }
      )
    },

    getCarList() {
      getCarSelectItems().then(res => {
        if (res.code === 200) {
          this.carList = res.data
        }
      })
    },
    handleQuery() {
      this.queryParams.pageNumber = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm('queryForm')
      this.queryParams.pageNumber = 1
      this.$refs.tables.sort(this.defaultSort.prop, this.defaultSort.order)
    },

    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop
      this.queryParams.isAsc = column.order
      this.getList()
    },
    /** 详细按钮操作 */
    handleView(row) {
      this.open = true
      this.form = row
    },

    /** 导出按钮操作 */
    handleExport() {
      // this.download('monitor/operlog/export', {
      //   ...this.day
      // }, `operlog_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>


